package com.teen9g.infoplatformserver.controller;

import com.github.pagehelper.PageInfo;
import com.teen9g.PageQueryDTO.AdvicePageQueryDTO;
import com.teen9g.dto.AdviceDTO;
import com.teen9g.entity.Advice;
import com.teen9g.infoplatformserver.service.AdviceService;
import com.teen9g.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/advice")
public class AdviceController {

    @Autowired
    private AdviceService adviceService;

    /**
     * 分页查询建议列表
     *
     * @param advicePageQueryDTO 分页查询参数
     * @return 建议列表
     */
    @GetMapping("/page")
    public Result<PageInfo<Advice>> page(AdvicePageQueryDTO advicePageQueryDTO) {
        log.info("分页查询建议列表:{}", advicePageQueryDTO);
        PageInfo<Advice> pageInfo = adviceService.page(advicePageQueryDTO);
        return Result.success(pageInfo);
    }

    /**
     * 添加建议
     *
     * @param adviceDTO 建议DTO
     * @return 结果
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody AdviceDTO adviceDTO) {
        log.info("添加建议:{}", adviceDTO);
        String result = adviceService.add(adviceDTO);
        return Result.success(result);
    }

    /**
     * 更新建议
     *
     * @param adviceDTO 建议DTO
     * @return 结果
     */
    @PutMapping("/update/{id}")
    public Result<String> update(@RequestBody AdviceDTO adviceDTO,
                                 @PathVariable("id") Integer id) {
        log.info("更新建议:{}", adviceDTO);
        String result = adviceService.update(adviceDTO, id);
        return Result.success(result);
    }

    /**
     * 更新建议状态（管理员限定）
     *
     * @param id 建议id
     * @return 结果
     */
    @PutMapping("/status/{id}")
    public Result<String> updateStatus(@PathVariable("id") Integer id,
                                       @RequestParam String status,
                                       @RequestParam String adviceRemark) {
        log.info("更新建议状态:{}", id);
        String result = adviceService.updateStatus(id, status, adviceRemark);
        return Result.success(result);
    }

    /**
     * 删除建议
     *
     * @param id 建议id
     * @return 结果
     */
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable("id") Integer id) {
        log.info("删除id为{}的建议", id);
        String result = adviceService.delete(id);
        return Result.success(result);
    }

    /**
     * 更新建议点赞数
     *
     * @param id 建议id
     * @return 结果
     */
    @PutMapping("/likeChange/{id}")
    public Result<String> likeChange(@PathVariable("id") Integer id) {
        log.info("更新id为{}的建议点赞数", id);
        String result = adviceService.likeChange(id);
        return Result.success(result);
    }

    @GetMapping("/checkLike")
    public Result<Integer> checkLike(@RequestParam Integer adviceId,
                                     @RequestParam Integer userId) {
        log.info("检查用户{}是否点赞了建议{}", userId, adviceId);
        Integer result = adviceService.checkLike(adviceId, userId);
        return Result.success(result);
    }

}
